package com.googlecode.asm_ide.project;

import com.googlecode.asm_ide.util.CustomFile;
import com.googlecode.asm_ide.util.OperatingSystem;
import java.beans.XMLEncoder;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Philipp Hell <philipp@hell.info>
 */
public class Project {
    private String name;
    private String lastPath;
    
    public Project() {
    }

    public String getLastPath() {
        return lastPath;
    }
    
    public String getName() {
        return name;
    }

    public void setLastPath(String lastPath) {
        this.lastPath = lastPath;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return this.getName();
    }
    
    public static Project createProject(String name, File path){
        Project p = new Project();
        p.setName(name);
        CustomFile f = new CustomFile(path.getPath().concat(OperatingSystem.getDirectorySeparator()).concat(name));
        p.setLastPath(f.getPath());
        if (f.mkdir()) {
            BufferedWriter bw = null;
            try {
                CustomFile src = new CustomFile(f.getPath().concat(OperatingSystem.getDirectorySeparator()).concat("src"));
                src.mkdir();
                CustomFile asm = new CustomFile(src.getPath().concat(OperatingSystem.getDirectorySeparator()).concat(p.getName()).concat(".asm"));
                bw = new BufferedWriter(new FileWriter(asm));
                bw.close();
                CustomFile bin = new CustomFile(f.getPath().concat(OperatingSystem.getDirectorySeparator()).concat("bin"));
                bin.mkdir();
                XMLEncoder en;
                CustomFile project = new CustomFile(f.getPath().concat(OperatingSystem.getDirectorySeparator()).concat("asm-ide-project.xml"));
                en = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(project)));
                en.writeObject(p);
                en.close();
            } catch (IOException ex) {
                Logger.getLogger(Project.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    bw.close();
                } catch (IOException ex) {
                    Logger.getLogger(Project.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return p;
    }
    
}
